using System;
using System.ComponentModel;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Narwhunderful.Aphelia.Framework.Graphics;

namespace Narwhunderful.Aphelia.Framework.UI
{

    public class UINavigableComponentCollection<T> : UIComponentCollection<T> where T : IUINavigableComponent
    {

        IComparer<T> _horizontalNavigableComponentComparer = new UIHorizontalNavigableComponentComparer<T>();
        protected virtual IComparer<T> HorizontalNavigableComponentComparer
        {
            get
            {
                return _horizontalNavigableComponentComparer;
            }
            set
            {
                _horizontalNavigableComponentComparer = value;
            }
        }

        IComparer<T> _verticalNavigableComponentComparer = new UIVerticalVerNavigableComponentComparer<T>();
        protected virtual IComparer<T> VerticalNavigableComponentComparer
        {
            get
            {
                return _verticalNavigableComponentComparer;
            }
            set
            {
                _verticalNavigableComponentComparer = value;
            }
        }

        List<T> _horizontalNavigableComponents = new List<T>();
        public virtual List<T> HorizontalNavigableComponents
        {
            get
            {
                return _horizontalNavigableComponents;
            }
        }

        List<T> _verticalNavigableComponents = new List<T>();
        public virtual List<T> VerticalNavigableComponents
        {
            get
            {
                return _verticalNavigableComponents;
            }
        }

        public UINavigableComponentCollection(UIManager manager, IUIContainerComponent parentComponent)
            : base(manager, parentComponent)
        {
        }

        public override void OnComponentAdded(T component)
        {
            base.OnComponentAdded(component);
            _horizontalNavigableComponents.Add(component);
            _verticalNavigableComponents.Add(component);
        }

        public override void OnComponentRemoved(T component)
        {
            base.OnComponentRemoved(component);
            _horizontalNavigableComponents.Remove(component);
            _verticalNavigableComponents.Remove(component);
        }

        public override void OnSort()
        {
            base.OnSort();
            if (_horizontalNavigableComponentComparer != null) _horizontalNavigableComponents.Sort(_horizontalNavigableComponentComparer);
            if (_verticalNavigableComponentComparer != null) _verticalNavigableComponents.Sort(_verticalNavigableComponentComparer);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _horizontalNavigableComponents.Clear();
                _verticalNavigableComponents.Clear();
            }
            base.Dispose(disposing);
        }

    }
    
}
